import $C from '@/common/config.js';
import $store from '@/store/index.js';
export default {
	common: {
		method: 'GET',
		header: {'content-type':'application/json'},
		data: {}
	},
	request(options = {}) {
		options.url = $C.webUrl + options.url
		options.method = options.method || this.common.method
		options.header = options.header || this.common.header
		return new Promise((res, rej) => {
			uni.request({
				...options,
				success: (result) => {
					if(options.native) {
						return res(result)
					}
					if (result.statusCode !== 200) {
						uni.showToast({
							title:'服务器连接异常',
							icon:'none'
						})
						return rej(result.data)
					}
					return res(result.data.data)
				},
				fail: (err) => {
					uni.showToast({
						title: err.errMsg,
						icon:'none'
					})
				}
			})
		})
	},
	get(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	post(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'post'
		return this.request(options)
	}
}